package com.qc.ayouth.shop.admin.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.qc.ayouth.shop.admin.constants.AdminResponseCode;
import com.qc.ayouth.shop.admin.service.user.AdminAuthService;
import com.qc.ayouth.shop.core.beans.bo.ServiceResultBO;
import com.qc.ayouth.shop.core.beans.vo.ResponseVO;
import com.qc.ayouth.shop.core.constants.ServiceConstants;
import com.qc.ayouth.shop.core.utils.MD5;
import com.qc.ayouth.shop.dbs.entity.TSystemAdmin;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 权限相关
 * Created by Haojg on 2020/7/5
 */
@RestController
@RequestMapping("/api/admin/auth")
@Validated
@Slf4j
public class AdminAuthController {

    @Autowired
    AdminAuthService adminAuthService;

    @PostMapping(value="login")
    public ResponseVO login(@RequestBody String jsonParam){
        log.info("user login start --> jsonParam:" + jsonParam);
        ResponseVO vo = new ResponseVO();
        JSONObject param = JSONObject.parseObject(jsonParam);
        String username = param.getString("username");
        String password = param.getString("password");

        // 用户登录验证
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            vo.badArgument();
            return vo;
        }

        Subject currentUser = SecurityUtils.getSubject();
//        currentUser.login(new UsernamePasswordToken(username, password));
        try {
            currentUser.login(new UsernamePasswordToken(username, password));
        } catch (UnknownAccountException uae) {
            log.warn("登录:用户帐号或密码不正确");
            vo.fail(AdminResponseCode.ADMIN_INVALID_ACCOUNT, "用户帐号或密码不正确");
            return vo;
        } catch (LockedAccountException lae) {
            log.warn("登录:用户帐号已锁定不可用");
            vo.fail(AdminResponseCode.ADMIN_INVALID_ACCOUNT, "用户帐号已锁定不可用");
            return vo;
        } catch (AuthenticationException ae) {
            log.warn("登录:认证失败");
            vo.fail(AdminResponseCode.ADMIN_INVALID_ACCOUNT, "认证失败");
            return vo;
        }

        currentUser = SecurityUtils.getSubject();
        TSystemAdmin admin = (TSystemAdmin) currentUser.getPrincipal();

        vo.add("username", admin.getUsername());
        vo.add("avatar", admin.getAvatar());
        vo.add("token", currentUser.getSession().getId());
        vo.ok();
        log.info("user login end");
        return vo;
    }

    @RequiresAuthentication
    @GetMapping("/info")
    public ResponseVO info() {
        log.info("info start");
        ResponseVO vo = new ResponseVO();
        Subject currentUser = SecurityUtils.getSubject();
        TSystemAdmin adminInfo = (TSystemAdmin) currentUser.getPrincipal();
        ServiceResultBO bo = adminAuthService.getAdminAuthInfo(adminInfo);
        if (bo.getCode() == ServiceConstants.SERVICE_RESULT_FAIL) {
            vo.fail(bo.getMsg());
            return vo;
        }
        vo.add("result", bo.getData().get("userAuthInfo"));
        log.info("info start");
        return vo;
    }

    @GetMapping("/test")
    public ResponseVO testing() {
        log.info("logout start");
        System.out.println(MD5.md5Encrypt("123456"));
        ResponseVO vo = new ResponseVO();
        vo.add("data", "这是测试");
        return vo;
    }
    /**
     * @description: 退出登录： shiro实现
     * @author: haojg
     * @date: 2020/7/6 10:25
     * @param
     * @return: com.qc.ayouth.shop.core.beans.vo.ResponseVO
     */
    @RequiresAuthentication
    @PostMapping("/logout")
    public ResponseVO logout() {
        log.info("logout start");
        ResponseVO vo = new ResponseVO();
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        vo.ok();
        log.info("logout end");
        return vo;
    }

    @GetMapping("/2step-code")
    public ResponseVO stepCode() {
        log.info("stepCode start");
        // 不用两步验证
        ResponseVO vo = new ResponseVO();
        vo.add("stepCode", false);
        vo.ok();
        log.info("stepCode end");
        return vo;
    }
}
